package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ware.annotation.Token;
import com.example.ware.model.po.Admin;
import com.example.ware.model.po.Employees;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.service.EmployeesService;
import com.example.ware.utils.Ujwt;
import io.netty.handler.ssl.PemX509Certificate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/Employees")
//员工的
public class EmployeesController {

    @Resource
    EmployeesService employeesService;

    @Autowired
    Ujwt ujwt;

    @Resource
    RedisTemplate redisTemplate;

    //    登录接口
    @PostMapping("/login")
    public ResponseData login(@RequestBody Employees employees) {
        System.out.println(employees);
        if (employees.getUsername() == null && employees.getPassword() == null) {
            return new ResponseData(null, false, "账号或密码为空", 201);
        }

        QueryWrapper<Employees> qw = new QueryWrapper<>();
        qw.eq("username", employees.getUsername());
        qw.eq("password", employees.getPassword());
        Employees e = employeesService.getOne(qw);

        if (e != null) {
            String token = ujwt.getoken(e);
//            redisTemplate.opsForHash().put(e.getId(),token,e);
//            redisTemplate.expire(token,100, TimeUnit.SECONDS);
//            第一个参数为redis的key，二为map的key，三为map的value
//            redisTemplate.opsForValue().set(token,e,1000*60, TimeUnit.SECONDS);
            redisTemplate.opsForValue().set("TOKEN_" + token, e, 100, TimeUnit.DAYS); //1天过期时间

            return new ResponseData(token, true, "登录成功", 200);

        } else {
            return new ResponseData("null", false, "账号或密码错误,请联系管理员", 404);
        }
    }



    
//    登出
    @GetMapping("/logout")
    public ResponseData logout(@RequestHeader String token) {
        System.out.println("555555555555555555555555555555");
        System.out.println(token);
        Boolean delete = redisTemplate.delete("TOKEN_" + token);

        if (delete) {
            return new ResponseData(null, true, "成功退出登录", 200);
        } else {
            return new ResponseData(null, false, "退出登录失败", 201);
        }
    }


//    搜索普通员工
    @GetMapping("getList")
    @Token
    public ResponseData getOne(Page p,String name){
        Page<Employees> employeesPage=new Page<>();
        if(name.equals("")){
            employeesPage=employeesService.page(p);
        }else{
            QueryWrapper<Employees> qw=new QueryWrapper<>();
            qw.like("username",name);
            employeesPage=employeesService.page(p,qw);
        }

        if(employeesPage!=null){
            return new ResponseData(employeesPage,true,"获取成功",200);
        }else{
            return new ResponseData(employeesPage,true,"获取失败",201);
        }
    }



//    创建普通用户
@PostMapping("add")
@Token
public ResponseData add(@RequestBody Employees employees){
   employees.setId(UUID.randomUUID().toString().replaceAll("-", ""));
    boolean save = employeesService.save(employees);

    if(save){
        return new ResponseData(null,true,"添加成功",200);
    }else{
        return new ResponseData(null,false,"添加失败",201);
    }
}

    //修改用户
    @PutMapping ("update")
    @Token
    public ResponseData update(@RequestBody Employees e){
        UpdateWrapper<Employees> qw=new UpdateWrapper<>();
        qw.eq("id",e.getId());
        qw.set("status",e.getStatus());

        boolean save = employeesService.update(qw);

        if(save){
            return new ResponseData(null,true,"修改成功",200);
        }else{
            return new ResponseData(null,false,"修改失败",201);
        }
    }



}
